#!/bin/sh
##############################################################################
#  OpenDomo: Security & Domotics
#  Copyright (C) 2008  Isidro Catalan <skarvin@gmail.com>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##############################################################################


# ----------------------------------------------------------------------------
# desc: Librearia de gestión para el demonio Crond
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.com
# date: Noviembre 2008
#
# CHANGES:
#
# ----------------------------------------------------------------------------

OD_CRON_FILE_NAME="od_tasks"
OD_CRON_PATH="/etc/cron.d"
TMP_FILE="/tmp/od_ulkhxaqki`date +%N`"

# ----------------------------------------------------------------------------
# setCronTask ()
# params:
# @script2Execute
# @time - Format (hh:mm)
# @frequency min / hourly / weekly / daily / monthly
# @user
# ----------------------------------------------------------------------------
setCronTask ()
{
   script2Execute="$1"
   time="$2"
   frequency="$3"
   user="$4"

   #wich cron do we use?   
   cron_file="$OD_CRON_PATH/$user/$OD_CRON_FILE_NAME"
   cron_line=`getCronLine "$script2Execute" "$time" "$frequency" "$user"`   
   mod="0"
   
   #1- check if file exists
   if [ ! -e "$cron_file" ]; then
      #TODO: Admin must have permissions to do this
      mkdir "$OD_CRON_PATH/$user"
      touch "$cron_file"
      chown "$user" "$cron_file"
      chmod 700 "$cron_file"
   fi

   #2- check whether the task exists
   while read line; do
      file2executeInCron=`echo "$line" | cut -d" " -f7-20`	 
      if [ "$script2Execute" = "$file2executeInCron" ]; then
         # modify line
         echo "$cron_line" >> $TMP_FILE	         
         mod=1
      else
         echo "$line" >> $TMP_FILE	 	       	       
      fi

   done < "$cron_file"

   #3- if exists modify it if not add
   if [ "$mod" = 0 ]; then
   	echo "$cron_line" >> $cron_file
   	rm $TMP_FILE
   else
      # swapeamos los ficheros
	   mv "$TMP_FILE" "$cron_file"
   fi   
   
}

# ----------------------------------------------------------------------------
# getCronLine ()
# params:
# @script2Execute
# @time - Format (hh:mm)
# @frequency min / hourly / weekly / daily / monthly
# @user
# ----------------------------------------------------------------------------
getCronLine ()
{
   script2Execute="$1"
   time="$2"
   frequency="$3"
   cron_line=""
   
   #hours and minutes
   hours=`echo $time | cut -d":" -f1`
   minutes=`echo $time | cut -d":" -f2`   
   
   case "$frequency" in
      min)
		   cron_line="\* \* \* \* \* $script2Execute";;
	   hourly)
		   cron_line="$minutes \* \* \* \* $script2Execute";;
	   daily)
		   cron_line="$minutes $hours \* \* \* $script2Execute";;
	   weekly)
		   cron_line="$minutes $hours \* \* 7 $script2Execute";;
	   monthly)
		   cron_line="$minutes $hours 1 \* \* $script2Execute";;
   esac

   echo $cron_line;
}

#TODO:
reloadCron ()
{
   echo "Not working yet"
}

#TODO:
# ----------------------------------------------------------------------------
# removeCronTask ()
# params:
# @script2Execute
# @user
# ----------------------------------------------------------------------------
removeCronTask ()
{
   echo "Not working yet"
}





